package com.example.springwebmvcdemo.controller;

import com.example.springwebmvcdemo.model.User;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//               @RestController    =   @Controller    +   @ResponseBody
@Controller
@RequestMapping("/response")
public class ResponseController {
    @RequestMapping("/returnHtmlPage")
    //@ResponseBody
    //不加上面的body,返回的是页面,加上body,返回的是数据.
    public String returnHtmlPage() {
        return "/Hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "ahhdhahahdahhh";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1>returnHtml</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public User returnJson() {
        User user = new User();
        user.setName("祸乱之源");
        user.setAge(18);
        return user;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public User setStatus(HttpServletResponse response) {
        User user = new User();
        user.setName("伐木机");
        user.setAge(20);
        response.setStatus(500);
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/setHeader1",produces = "application/json")
    public String setHeader1() {
        return "{\"success\":true}";
    }

    @ResponseBody
    @RequestMapping("/setHeader2")
    public String setHeader2(HttpServletResponse response) {
        response.setHeader("My-Key","myvalue");
        Cookie cookie = new Cookie("key","value");
        response.addCookie(cookie);
        return "success";
    }
}
